{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a0c15e40",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1da6221",
   "metadata": {},
   "source": [
    "1.读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "219a6328",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Species</th>\n",
       "      <th>Length3</th>\n",
       "      <th>Height</th>\n",
       "      <th>Width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Bream</td>\n",
       "      <td>30.0</td>\n",
       "      <td>11.5200</td>\n",
       "      <td>4.0200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Bream</td>\n",
       "      <td>31.2</td>\n",
       "      <td>12.4800</td>\n",
       "      <td>4.3056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Bream</td>\n",
       "      <td>31.1</td>\n",
       "      <td>12.3778</td>\n",
       "      <td>4.6961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Bream</td>\n",
       "      <td>33.5</td>\n",
       "      <td>12.7300</td>\n",
       "      <td>4.4555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Bream</td>\n",
       "      <td>34.0</td>\n",
       "      <td>12.4440</td>\n",
       "      <td>5.1340</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Species  Length3   Height   Width\n",
       "0   Bream     30.0  11.5200  4.0200\n",
       "1   Bream     31.2  12.4800  4.3056\n",
       "2   Bream     31.1  12.3778  4.6961\n",
       "3   Bream     33.5  12.7300  4.4555\n",
       "4   Bream     34.0  12.4440  5.1340"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('./Fish_pre.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "276318b8",
   "metadata": {},
   "source": [
    "2.特征缩放-特征编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "90267590",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['Species'] = pd.factorize(df['Species'])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "df35e492",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Species</th>\n",
       "      <th>Length3</th>\n",
       "      <th>Height</th>\n",
       "      <th>Width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>11.5200</td>\n",
       "      <td>4.0200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>31.2</td>\n",
       "      <td>12.4800</td>\n",
       "      <td>4.3056</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>31.1</td>\n",
       "      <td>12.3778</td>\n",
       "      <td>4.6961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>33.5</td>\n",
       "      <td>12.7300</td>\n",
       "      <td>4.4555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>12.4440</td>\n",
       "      <td>5.1340</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Species  Length3   Height   Width\n",
       "0        0     30.0  11.5200  4.0200\n",
       "1        0     31.2  12.4800  4.3056\n",
       "2        0     31.1  12.3778  4.6961\n",
       "3        0     33.5  12.7300  4.4555\n",
       "4        0     34.0  12.4440  5.1340"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6d44f1f",
   "metadata": {},
   "source": [
    "3.读取模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9ca2002e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import joblib\n",
    "\n",
    "#用'15综合大实验手册-鱼的重量预测.ipynb'生成的模型去预测鱼的重量\n",
    "model = joblib.load('./fish_refrssor.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e73d8df4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python310\\site-packages\\sklearn\\base.py:443: UserWarning: X has feature names, but KNeighborsRegressor was fitted without feature names\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1447.22279777, 1446.90694844, 1446.93054142, 1446.84991077,\n",
       "       1446.91622325, 1446.61660413, 1446.46028814, 1446.8606982 ,\n",
       "       1446.52061266, 1446.4773497 , 1446.46674311, 1446.44548376,\n",
       "       1446.59625014, 1446.55304155, 1446.32042831, 1446.20923563,\n",
       "       1446.34977291, 1446.33621004, 1446.19575401, 1446.4252755 ,\n",
       "       1446.29895969, 1446.12918837, 1446.22717639, 1446.23191281,\n",
       "       1446.09585738, 1446.07290345, 1446.06877412, 1446.05356822,\n",
       "       1445.98598713, 1445.65766787, 1445.82892588, 1445.82068425,\n",
       "       1445.73331303, 1445.75700264, 1445.90619389, 1445.73937195,\n",
       "       1443.8866764 , 1443.58191199, 1443.30189933, 1443.2762477 ,\n",
       "       1443.09119589, 1443.03831359, 1442.89017671, 1442.68913821,\n",
       "       1442.73076482, 1442.50287817, 1442.57490279, 1442.5980079 ,\n",
       "       1442.11986438, 1442.23148587, 1442.20440333, 1442.02022918,\n",
       "       1441.75340795, 1441.57485057, 1441.03768596, 1441.84105134,\n",
       "       1441.76102697, 1441.53515239, 1441.10797422, 1440.58419015,\n",
       "       1440.30044099, 1444.92078171, 1444.63278984, 1443.72149524,\n",
       "       1443.19992465, 1442.90313858, 1442.77118139, 1442.68376148,\n",
       "       1442.46607538, 1442.13527749, 1441.76429477, 1441.615421  ,\n",
       "       1451.95070543, 1446.27393043, 1445.56870012, 1444.93057927,\n",
       "       1444.42209172, 1444.12981919, 1444.0531516 , 1443.7498762 ,\n",
       "       1443.60347135, 1443.53169848, 1443.12053426, 1443.09753448,\n",
       "       1443.12440118, 1443.00195119, 1442.84849849, 1442.8764092 ,\n",
       "       1442.85282471, 1442.86515647, 1442.89591892, 1442.75463548,\n",
       "       1442.7044335 , 1442.70579944, 1442.66672291, 1442.52386344,\n",
       "       1442.37429334, 1442.42172906, 1442.2812897 , 1442.22485525,\n",
       "       1442.14466227, 1441.95234366, 1441.8141594 , 1441.81506489,\n",
       "       1441.8185822 , 1441.81563855, 1441.7166568 , 1441.63274889,\n",
       "       1441.43404704, 1441.10427444, 1440.85851133, 1440.76078181,\n",
       "       1440.63543261, 1440.6969874 , 1440.60163391, 1440.61519326,\n",
       "       1440.45373372, 1440.44570995, 1440.43594423, 1440.37705875,\n",
       "       1440.36765413, 1440.36363173, 1440.37411652, 1440.21630346,\n",
       "       1440.19552878, 1440.1705596 , 1440.14308578, 1440.1093946 ,\n",
       "       1441.02975506, 1440.75037891, 1440.66577786, 1440.57610021,\n",
       "       1440.52629363, 1440.48071376, 1440.17609358, 1440.17790858,\n",
       "       1440.16640803, 1440.03629192, 1439.99783517, 1439.87479144,\n",
       "       1439.70467027, 1439.5306751 , 1439.38507677, 1439.38507677,\n",
       "       1439.27596673, 1447.40864299, 1447.01440882, 1446.99961818,\n",
       "       1446.86874274, 1446.61137098, 1446.50180591, 1446.2372783 ,\n",
       "       1446.20211287, 1446.14478856, 1446.10871251, 1446.02530464,\n",
       "       1445.8708836 , 1445.35652843, 1444.8913409 ])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(df)   #这里就预测出来了所有行的重量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "33fb7b26",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lenovo\\AppData\\Roaming\\Python\\Python310\\site-packages\\sklearn\\base.py:443: UserWarning: X has feature names, but KNeighborsRegressor was fitted without feature names\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "df['Weight'] = model.predict(df)   #把预测重量的这一列添加到原来的df中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8e12ba95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Species</th>\n",
       "      <th>Length3</th>\n",
       "      <th>Height</th>\n",
       "      <th>Width</th>\n",
       "      <th>Weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>11.5200</td>\n",
       "      <td>4.0200</td>\n",
       "      <td>1447.222798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>31.2</td>\n",
       "      <td>12.4800</td>\n",
       "      <td>4.3056</td>\n",
       "      <td>1446.906948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>31.1</td>\n",
       "      <td>12.3778</td>\n",
       "      <td>4.6961</td>\n",
       "      <td>1446.930541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>33.5</td>\n",
       "      <td>12.7300</td>\n",
       "      <td>4.4555</td>\n",
       "      <td>1446.849911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>12.4440</td>\n",
       "      <td>5.1340</td>\n",
       "      <td>1446.916223</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Species  Length3   Height   Width       Weight\n",
       "0        0     30.0  11.5200  4.0200  1447.222798\n",
       "1        0     31.2  12.4800  4.3056  1446.906948\n",
       "2        0     31.1  12.3778  4.6961  1446.930541\n",
       "3        0     33.5  12.7300  4.4555  1446.849911\n",
       "4        0     34.0  12.4440  5.1340  1446.916223"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
